<?php 
    $proxyaccount;

    $useProxy = false;
    $ProxyServer = "proxy.netwerk.intern:8080";
    $ProxyUser = "r.kapaan";
    $ProxyPass = "Datawarehouse6";

    function getContext($overwriteProxy = false, $server = null, $user = null, $pass = null){
        if($overwriteProxy && strlen($server) > 0) {
            $useProxy = true;
            $ProxyServer = $server;
            $ProxyUser = $user;
            $ProxyPass = $pass;
        }
        
        if ($useProxy == true) {
                if($ProxyUser && $ProxyPass) {
                    $auth = base64_encode($ProxyUser.':'.$ProxyPass); 
                    $header= array("Proxy-Authorization: Basic $auth", "Authorization: Basic $auth") ;
                } else {
                    $header = array();
                } 
                $opts = array( 
                               'http' => array ( 
                                               'method'=>'GET', 
                                               'proxy'=> $ProxyServer, 
                                               'request_fulluri' => true, 
                                               'header'=> $header

                               ), 
                               'https' => array ( 
                                               'method'=>'GET', 
                                               'proxy'=> $ProxyServer, 
                                               'request_fulluri' => true, 
                                               'header'=> $header
                               ) 
                ); 
            return stream_context_create($opts); 
        } 
        
        return null;
    }


    require_once( 'includes/classes/class-p2c-platform-list-table.php' );
    require_once( 'includes/classes/class-p2c-platform.php' );
    require_once( 'includes/classes/class-p2c-batch.php' );
    require_once( 'includes/classes/class-p2c-project-list-table.php' );
    require_once( 'includes/classes/class-p2c-project-data-list-table.php' );
    require_once( 'includes/classes/class-p2c-project.php' );
    require_once( 'includes/classes/class-p2c-project-data.php' );

    /*********************
    P2C ADMIN MENU
    *********************/


    function p2c_admin() {
        add_menu_page( 'Proud2Crowd Admin Module', 'P2C Admin', 'manage_options', 'p2c', 'p2c_admin_settings');

        add_submenu_page('p2c', 'Manage Platforms', 'Platforms', 'manage_options', 'p2cpf', 'p2c_admin_platform');
        
        add_submenu_page('p2c', 'Manage Projects', 'Projects', 'manage_options', 'p2cpr', 'p2c_admin_project');
    }
    // add custom p2c menu
    add_action( 'admin_menu', 'p2c_admin');

    function p2c_admin_settings() {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        //include_once('library\p2c_admin.php');
    }

    function p2c_admin_platform() {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        include_once('p2c_admin_platforms.php');
    }

    function p2c_admin_project() {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        include_once('p2c_admin_projects.php');
    }

    function p2c_current_action() {
        if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] )
            return $_REQUEST['action'];

        if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] )
            return $_REQUEST['action2'];

        return false;
    }

    function p2c_platform( $id ) {        
        $platform = new P2C_Platform( $id );

        return $platform;
    }

    function p2c_project( $id ) {        
        $platform = new P2C_Project( $id );

        return $platform;
    }

    function saveProjectData($external_id, $platform, $batch, $current, $days, $worth, $shares, $target, $image, $link, $name, $description, $active, $currency) {
        $project = P2C_Project::get( $external_id, $platform, $batch );
        $project->image = $image;
        $project->url = $link;
        $project->name = $name;
        $project->description = $description;
        $project->active = $active;
        $project->currency = $currency;

        $project->save();
        
        if(!$project->initial) {
            $pdata = P2C_ProjectData::get($project);
            
            if($pdata->current != $current || $pdata->days_remaining != $days || $pdata->estimated_worth != $worth || $pdata->shares != $shares || $pdata->target != $target) {
                $pdata = P2C_ProjectData::create($project, $batch, $current, $days, $worth, $shares, $target);
                $pdata->save();
            }
        }
        
        echo '<br/>Project ingeladen: ' . $project->name;
    }

    function p2c_getProjects($page = null) {
        $platformId = 0;
        if(isset($_GET['pid'])) {
            $platformId = $_GET['pid'];
        }
        $country = '';
        if(isset($_GET['c'])) {
            $country = $_GET['c'];
        }
        $showInactive = 0;
        if(isset($_GET['i'])) {
            $showInactive = $_GET['i'];
        }
        $showFunded = 0;
        if(isset($_GET['f'])) {
            $showFunded = $_GET['f'];
        }
        
        $orderby = 'import_date';
        if(isset($_GET['so'])) {
            $orderby = $_GET['so'];
        }
        $offset = 0;
        if(isset($_GET['of'])) {
            $offset = $_GET['of'];
        }
        $postperpage = $page == 'find' ? 12 : 8;
        if(isset($_GET['pp'])) {
            $postperpage = $_GET['pp'];
        }
        $order = 'DESC';
        if(isset($_GET['dir'])) {
            $order = $_GET['dir'];
        }
        
        $items = array();

        $args = array(
                    'posts_per_page' => $postperpage,
                    'orderby' => $orderby,
                    'order' => $order,
                    'offset' => $offset );

        $items = P2C_Project::findBy( $args, $platformId, $country, $showInactive, $showFunded );
                        
        $html = '';
        
        if($page == 'main') {
            if( $orderby != 'import_date' )
                $html = $html . '<a href="?so=import_date&dir=desc">MOST RECENT</a> | ';
            else
                $html = $html . 'MOST RECENT |';
            
            if( $orderby != 'percentage' )
                $html = $html . '<a href="?so=percentage&dir=desc">TRENDING</a> | ';
            else
                $html = $html . 'TRENDING |';
            
            if( $orderby != 'target' )
                $html = $html . '<a href="?so=target&dir=desc">BIGGEST</a>';
            else
                $html = $html . 'BIGGEST';
        }
        
        if($page == 'find') {
            $html = '<script type="text/javascript">function setPaging(val) { document.getElementById(\'currentOffset\').value=val;document.getElementById(\'formProjects\').submit(); }</script>
            <form action="/wordpress/find-projects/" method="GET" id="formProjects">';
            $itemcount = P2C_Project::countBy( $platformId, $country, $showInactive, $showFunded );
            
            $paging = '';
            $lastpage = ceil($itemcount/$postperpage); 
            if($lastpage > 1) {
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    $pnr = ($counter - 1) * $postperpage;
                    if($offset == $pnr) {
                        $paging = $paging . '<strong>' . $counter . '</strong>';
                    } else {
                        $paging = $paging . '<a href="#" onclick="setPaging(' . $pnr . ');">' . $counter . '</a>';
                    }
                    
                    if($counter < $lastpage)
                        $paging = $paging . ' | ';
                }
                
                if($offset != 0) {
                    $paging = '<a href="#" onclick="setPaging(' . ($offset - $postperpage) . ');">< </a>' . $paging;
                    $paging = '<a href="#" onclick="setPaging(0);"><< </a>' . $paging;
                }
                if($offset != ($lastpage - 1) * $postperpage){
                    $paging = $paging . '<a href="#" onclick="setPaging(' . ($offset + $postperpage) . ');"> ></a>';
                    $paging = $paging . '<a href="#" onclick="setPaging(' . (($lastpage - 1) * $postperpage) . ');"> >>></a>';
                }
            }
            
            $html = $html . '<input type="hidden" name="of" id="currentOffset" value="'.$offset.'"/>
                <table align="center"><tr>
                <td>FILTER PROJECTS:</td>
                <td><select class="form-element form-element-175" id="platform" name="pid">
                <option value="0" ' . ($platformId == 0 ? 'selected' : '') . '>All platforms</option>
                <option value="4" ' . ($platformId == 4 ? 'selected' : '') . '>Symbid</option>
                </select></td>
                <td>
                <select class="form-element form-element-175" id="country" name="c">
                <option value="" ' . ($country == '' ? 'selected' : '') . '>All countries</option>
                <option value="us" ' . ($country == 'us' ? 'selected' : '') . '>USA</option>
                <option value="uk" ' . ($country == 'uk' ? 'selected' : '') . '>United Kingdom</option>
                </select></td>
                <td>
                <select class="form-element form-element-175" id="inactiveprojects" name="i">
                <option value="0" ' . ($showInactive == 0 ? 'selected' : '') . '>Hide inactive projects</option>
                <option value="1" ' . ($showInactive == 1 ? 'selected' : '') . '>Show inactive projects</option>
                </select></td>
                <td>
                <select class="form-element form-element-175" id="fundedprojects" name="f">
                <option value="0" ' . ($showFunded == 0 ? 'selected' : '') . '>Hide funded projects</option>
                <option value="1" ' . ($showFunded == 1 ? 'selected' : '') . '>Show funded projects</option>
                </select></td>
                </tr>
                <tr>
                <td>SORT PROJECTS:</td>
                <td>
                <select class="form-element form-element-175" id="fundedprojects" name="so">
                <option value="import_date" ' . ($orderby == 'import_date' ? 'selected' : '') . '>Date</option>
                <option value="target" ' . ($orderby == 'target' ? 'selected' : '') . '>Target</option>
                <option value="percentage" ' . ($orderby == 'percentage' ? 'selected' : '') . '>Funding percentage</option>
                <option value="name" ' . ($orderby == 'name' ? 'selected' : '') . '>Name</option>
                </select>
                </td>
                <td>
                <select class="form-element form-element-175" id="fundedprojects" name="dir">
                <option value="ASC" ' . ($order == 'ASC' ? 'selected' : '') . '>Ascending</option>
                <option value="DESC" ' . ($order == 'DESC' ? 'selected' : '') . '>Descending</option>
                </select>
                </td>
                <td>
                <select class="form-element form-element-175" id="projecspp" name="pp">
                <option value="12" ' . ($postperpage == '12' ? 'selected' : '') . '>12</option>
                <option value="30" ' . ($postperpage == '30' ? 'selected' : '') . '>30</option>
                <option value="60" ' . ($postperpage == '60' ? 'selected' : '') . '>60</option>
                <option value="10000" ' . ($postperpage == '10000' ? 'selected' : '') . '>All</option>
                </select>
                </td>
                <td><input class="form-button search" type="submit" value="Show projects" id="showProjects" /></td>
                </tr></table>
                </form>';
            
            $html = $html . '<p>&nbsp;</p>';
            $html = $html . '<div class="text-center">' . $paging . '</div>';
        }
    
        $html = $html . '<div class="project_overview">';

        foreach ( $items as $project ) {
            $project->getData();
            $pdata = $project->projectdata;

            $html = $html . '<div class="project">';
            $html = $html . '<div class="project_image project_div"><a href="'.$project->url.'" target="_blank"><img alt="'.$project->name.'" src="'.$project->image.'" /></a></div>';
            $html = $html . '<div class="project_daysremaining">'.$pdata->days_remaining.'</div>';
            $html = $html . '<div class="project_days">days</div>';
            $html = $html . '<div class="project_title project_div">'.$project->name.'</div>';
            $html = $html . '<div class="project_description project_div">'.$project->description.'</div>';
            $html = $html . '<div class="project_info project_div">'; 
            $html = $html . '<div class="project_progressbar_grey" style="width: 100%;">';
            $html = $html . '<div class="project_progressbar_blue" style="width: '.round($pdata->percentage).'%;"></div></div>';
            $html = $html . $project->currency.' '.number_format($pdata->current,0). ' / ' . $project->currency.' '.number_format($pdata->target, 0).'<span class="project_percentage">'.round($pdata->percentage).'%</span></div>';
            $html = $html . '<div class="project_div project_links"><a href="'.$project->url.'" target="_blank" class="project_btnleft">Invest</a><a href="find-projects/?pid='.$project->platform_id.'" class="project_btnright">More '.$project->platform.' &raquo;</a></div>';
            $html = $html . '</div>';
        }
        
        
        $html = $html . '</div>';
        $html = $html . '<div class="text-center">' . $paging . '</div>';
        
        return $html;
    }

?>
